package com.autohammal.implement;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.widgets.Text;

import com.autohammal.ITask;
import com.autohammal.target.Target;
import com.autohammal.ui.frame.items.ItemDescriptor;
import com.autohammal.util.RegexUtil;

public class FileCheckTarget extends Target {

	private static final long serialVersionUID = 3117867899653191961L;

	public FileCheckTarget() {
	}

	@Override
	public List<ITask> createTask() {
		File targetDir = new File(getConfigurationItem("targetDir").toString());
		File desDir = new File(getConfigurationItem("desDir").toString());
		String rule = (String) getConfigurationItem("fileType");
		List<ITask> list = null;
		for (File file : targetDir.listFiles()) {
			if (RegexUtil.isMatch(rule, file.getName())) {
				if (list == null)
					list = new ArrayList<ITask>();
				list.add(newTask(file.getAbsolutePath(),
						desDir.getAbsolutePath()));
			}
		}
		if (list == null)
			return null;
		return list;
	}

	ITask newTask(String targetFile, String desFile) {
		ITask task = new FileMoveTask(this);
		task.setTaskName(targetFile);
		return task;
	}

	@Override
	public ItemDescriptor[] configurationDescriptors() {
		return new ItemDescriptor[] {
				ItemDescriptor.createItemDescriptor(FileCheckTarget.class,
						"targetDir", String.class, new Object[] {
								ItemDescriptor.PROPERTYEDITORCLASS, Text.class,
								ItemDescriptor.DISPLAYNAME, "Source Directory",
								ItemDescriptor.SHORTDESCRIPTION,
								"Directory will be monitored." }),

				ItemDescriptor
						.createItemDescriptor(
								FileCheckTarget.class,
								"desfile",
								String.class,
								new Object[] {
										ItemDescriptor.PROPERTYEDITORCLASS,
										Text.class, ItemDescriptor.DISPLAYNAME,
										"Destination Directory",
										ItemDescriptor.SHORTDESCRIPTION,
										"Directory will be moved to from monitored directory." }),
				ItemDescriptor
						.createItemDescriptor(
								FileCheckTarget.class,
								"fileType",
								String.class,
								new Object[] {
										ItemDescriptor.PROPERTYEDITORCLASS,
										Text.class, ItemDescriptor.DISPLAYNAME,
										"Monitor File Type",
										ItemDescriptor.SHORTDESCRIPTION,
										"File which type will be moved to destination directory." }) };
	}

	@Override
	public ItemDescriptor[] inputDescriptors() {
		return null;
	}

	@Override
	public ItemDescriptor[] outputDescriptors() {
		return null;
	}

}
